import ITaskData from "../types/ITaskData"
import ITask from "../types/ITask"
import localCache from "../../../utils/cache"
import { UNDERWAYTASKID, COMPLETEDTASKID } from "../constant/taskId"
import { taskName } from "../enum/taskName"

/**
 * 任务类型切换 eg：进行中->已完成
 * @param task 要切换的任务
 * @param taskData 任务数据数组
 * @param taskType 任务类型
 */
export const change = (task: ITask, taskData: ITaskData, taskType: string): void => {
  switch(taskType){
    case taskName.UNDERWAY: 
      taskData.completedTaskData.push(task)
      taskData.underwayTaskData = taskData.underwayTaskData.filter( item => item.id !== task.id)
      break
    case taskName.COMPLETED:
      taskData.underwayTaskData.push(task)
      taskData.completedTaskData = taskData.completedTaskData.filter( item => item.id !== task.id)
      break
  }

  localCache.clearCache()
  taskData.underwayTaskData.forEach(item => {
    localCache.setCache(UNDERWAYTASKID + item.id, item.title)
  })
  taskData.completedTaskData.forEach(item => {
    localCache.setCache(COMPLETEDTASKID + item.id, item.title)
  })
}